package com.liuyfly.leetcode.questions;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liuyangfang at 2022-09-02 13:43
 */
@Slf4j
public class Question0557 {
    /**
     * 给定一个字符串 s ，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。 示例 1： 输入：s = "Let's take
     * LeetCode contest" 输出："s'teL ekat edoCteeL tsetnoc" 示例 2: 输入： s = "God Ding"
     * 输出："doG gniD" <br/>
     * <br/>
     * 链接：https://leetcode.cn/problems/reverse-words-in-a-string-iii
     */
    public static String reverseWords(String s) {
        StringBuilder builder = new StringBuilder();
        String[] words = s.split(" ");
        for (String word : words) {
            builder.append(reverseWord(word)).append(" ");
        }
        return builder.toString().trim();
    }

    public static String reverseWord(String word) {
        StringBuilder builder = new StringBuilder();
        char[] letters = word.toCharArray();
        for (int i = letters.length - 1; i >= 0; i--) {
            builder.append(letters[i]);
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        String s = "I Am The World Who Are You";
        String result = reverseWords(s);
        log.info("result:[{}]", result);
    }
}
